'''
Created on 29 Apr 2011

@author: serj
'''
import logging
import simplejson
import traceback
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class WebInterfaceHandler(webapp.RequestHandler):
    def __init__(self):
        self.actions = {}
#        self.actions['getAccessToken'] = (self.getAccessToken, 'text/html')
        self.actions['login'] = (self.login, 'text/html')
        self.actions['ll'] = (self.ll, 'text/html')
        self.path = ''
        
    def login(self):
#        self.redirect(self.request.scheme + '://' + self.request.host + '/service/Blogger/authCheck?userId=userId&waveId=waveId')
#        return '<body onload="window.open(\'' + self.request.scheme + '://' + self.request.host + '/service/Blogger/authCheck?userId=userId&waveId=waveId' + '\')"></body>'
        return '''
        <body>
            <script type="text/javascript">
                var wnd;
                function wndLoad(){
                    wnd = window.open(\'''' + self.path + 'service/Blogger/authCheck?userId=userId&waveId=waveId' + '''\', '', 'height=500,width=600,toolbar=no,directories=no,personalbar=no');
                }
                function wndClose(){
                    if(wnd && !wnd.closed){
                        wnd.close();
                    }
                }
            </script>
            <button id="btn" onclick="wndLoad()">Test</button>
        </body>'''
    
    def ll(self):
        return '''
        <body>
            <script type="text/javascript">
                function wndLoad(){
                    $(window).unload(function(){
                            alert(window.closed);
                    });
                }
            $(document).ready(function(){wndLoad()});
            </script>
        </body>
        '''
    
    def get(self, action):
        (handler, mime_type) = self.actions.get(action, (None, None))
        if handler:
            self.path = self.request.scheme + '://' + self.request.host + '/'
            script = '<script src="%s"></script>' % (self.path + 'static/js/jquery-1.5.1.min.js')
            script += '<script src="%s"></script>' % (self.path + 'static/js/jquery.tmpl.min.js')
            script += '<script src="%s"></script>' % (self.path + 'static/js/face.js')
            if mime_type:
#                self.response.headers['Cache-Control'] = 'private, no-cache'
#                self.response.headers['Expires'] = 'Wed, 17 Sep 1975 21:32:10 GMT'
                self.response.headers['Content-Type'] = mime_type
            body = handler()
            if not body:
                body = ''
            logging.info(BASE_PAGE)
            logging.info(script)
            logging.info(body)
            logging.info(BASE_PAGE % (script, body))
            self.response.out.write(BASE_PAGE % (script, body))
        else:
            logging.info('Request did not match any handler')
    
    def post(self):
        pass

BASE_PAGE = '''
<html>
    <head>
        <title></title>
        %s
    </head>
    %s
</html>
'''

if __name__ == '__main__':
    app = webapp.WSGIApplication([
        ('/interface/(.*)', WebInterfaceHandler),
    ], debug=True)
    run_wsgi_app(app)